﻿/*----------------------------------------------------------------
// Copyright (C) 2011 上海
// 版权所有。 
//
// 文件名：YaxMachineInfo
// 文件功能描述：设备管理
//
// 
// 创建标识：2012-02-13 王冲
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DAO.WaiGaoQiao;

namespace Model.WaiGaoQiao
{
    public class YaxMachineInfo
    {
        /// <summary>
        /// 主键，设备MacID
        /// </summary>
        public int MacID
        {
            get;
            set;
        }
        /// <summary>
        /// 设备组ID
        /// </summary>
        public int NodeID
        {
            get;
            set;
        }
        /// <summary>
        /// 设备编号
        /// </summary>
        public int MacNo
        {
            get;
            set;
        }
        /// <summary>
        /// 设备类型ID
        /// </summary>
        public int MacType
        {
            get;
            set;
        }
        /// <summary>
        /// 通讯方式
        /// </summary>
        public int CommStyle
        {
            get;
            set;
        }
        /// <summary>
        /// 通讯方式（串口、TCP/IP）
        /// </summary>
        public string CommStyle_
        {
            get;
            set;
        }
        /// <summary>
        /// 设备组名称
        /// </summary>
        public string NodeName
        {
            get;
            set;
        }
        /// <summary>
        /// 设备类型名称
        /// </summary>
        public string MacTypeName
        {
            get;
            set;
        }
        /// <summary>
        /// 端口号
        /// </summary>
        public int PortNo
        {
            get;
            set;
        }
        /// <summary>
        /// IP地址
        /// </summary>
        public string IPAddress
        {
            get;
            set;
        }
        /// <summary>
        /// 网关
        /// </summary>
        public string GetwayIP
        {
            get;
            set;
        }
        /// <summary>
        /// 波特率
        /// </summary>
        public int BaudRate
        {
            get;
            set;
        }
        /// <summary>
        /// 设备描述
        /// </summary>
        public string MacDescribe
        {
            get;
            set;
        }
        /// <summary>
        /// 子网掩码
        /// </summary>
        public string SubIPAddr
        {
            get;
            set;
        }

        private DataAccessYaxMachine dar;
        private int YaxMachinecount;
        public int _YaxMachinecount
        {
            get { return YaxMachinecount; }
        }
        public YaxMachineInfo(bool isgetcount)
        {
            dar = new DataAccessYaxMachine();
            if (isgetcount)
            {
                YaxMachinecount = GetYaxMachineCount();
            }
        }
        public YaxMachineInfo()
        { }

        /// <summary>
        /// 获取设备数量
        /// </summary>
        /// <returns></returns>
        private int GetYaxMachineCount()
        {
            return dar.GetYaxMachineCount();
        }

        /// <summary>
        /// 获取设备信息（分页）
        /// </summary>
        /// <param name="pagesize"></param>
        /// <param name="startindex"></param>
        /// <returns></returns>
        public IList<YaxMachineInfo> SelYaxMachineInfo(int pagesize, int startindex, string strquery)
        {
            IList<YaxMachineInfo> yaxlist = new List<YaxMachineInfo>();
            YaxMachineInfo yaxinfo;
            DataSet ds = dar.SelYaxMachineInfo(pagesize, startindex, strquery);
            foreach (DataRow item in ds.Tables[0].Rows)
            {
                yaxinfo = new YaxMachineInfo()
                {
                    MacID = Convert.ToInt32(item["MacID"]),
                    NodeName = item["NodeName"].ToString(),
                    MacNo = Convert.ToInt32(item["MacNo"]),
                    MacTypeName = item["MacTypeName"].ToString(),
                    CommStyle_ = item["CommStyle_"].ToString(),
                    PortNo = Convert.ToInt32(item["PortNo"]),
                    BaudRate = Convert.ToInt32(item["BaudRate"]),
                    IPAddress = item["IPAddress"].ToString(),
                    GetwayIP = item["GetwayIP"].ToString(),
                    SubIPAddr = item["SubIPAddr"].ToString(),
                    MacDescribe = item["设备描述"].ToString(),
                };
                yaxlist.Add(yaxinfo);
            }
            return yaxlist;
        }

        /// <summary>
        /// 删除设备
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public int DeleteYaxMachine(string id)
        {
            return dar.DeleteYaxMachine(id);
        }

        /// <summary>
        /// 获取设备组列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public DataSet GetNodeName()
        {
            return dar.GetNodeName();
        }

        /// <summary>
        /// 获取设备类型列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public DataSet GetMacTypeName()
        {
            return dar.GetMacTypeName();
        }

        public int SaveYaxMachine(YaxMachineInfo yax)
        {
            object[] obj = new object[] { yax.NodeID, yax.MacNo, yax.MacDescribe, yax.MacType, yax.CommStyle, yax.PortNo, yax.BaudRate, yax.IPAddress, yax.GetwayIP, yax.SubIPAddr };
            return dar.SaveYaxMachine(obj);
        }

        /// <summary>
        /// 保存设备(修改界面下)
        /// </summary>
        /// <param name="rolepara"></param>
        /// <returns></returns>
        public int SaveYaxMachineEdit(YaxMachineInfo yax)
        {
            object[] obj = new object[] { yax.MacID, yax.NodeID, yax.MacNo, yax.MacDescribe, yax.MacType, yax.CommStyle, yax.PortNo, yax.BaudRate, yax.IPAddress, yax.GetwayIP, yax.SubIPAddr };
            return dar.SaveYaxMachineEdit(obj);
        }

        /// <summary>
        /// 根据编号获取一条设备信息
        /// </summary>
        /// <param name="supplierNo"></param>
        /// <returns></returns>
        public YaxMachineInfo GetYaxMachineInfoOne(string id)
        {
            dar = new DataAccessYaxMachine();
            DataSet ds = dar.GetYaxMachineInfoOne(id);
            YaxMachineInfo info = new YaxMachineInfo();
            info.MacID = int.Parse(ds.Tables[0].Rows[0]["MacID"].ToString());
            info.NodeID = Convert.ToInt32(ds.Tables[0].Rows[0]["NodeID"]);
            info.MacNo = Convert.ToInt32(ds.Tables[0].Rows[0]["MacNo"]);
            info.MacDescribe = ds.Tables[0].Rows[0]["MacDescribe"].ToString();
            info.MacType = Convert.ToInt32(ds.Tables[0].Rows[0]["MacType"]);
            info.CommStyle = Convert.ToInt32(ds.Tables[0].Rows[0]["CommStyle"]);
            info.PortNo = Convert.ToInt32(ds.Tables[0].Rows[0]["PortNo"]);
            info.BaudRate = Convert.ToInt32(ds.Tables[0].Rows[0]["BaudRate"]);
            info.IPAddress = ds.Tables[0].Rows[0]["IPAddress"].ToString();
            info.GetwayIP = ds.Tables[0].Rows[0]["GetwayIP"].ToString();
            info.SubIPAddr = ds.Tables[0].Rows[0]["SubIPAddr"].ToString();
            return info;
        }

        public bool CheckMacNo(int MacNo, int OldMacNo, string AddEdit)
        {
            int count = dar.CheckMacNo(MacNo, OldMacNo, AddEdit);
            return count > 0 ? false : true;
        }


        public bool CheckMacNoAdd(int MacNoAdd)
        {
            int count = dar.CheckMacNoAdd(MacNoAdd);
            return count > 0 ? false : true;
        }

        /// <summary>
        /// 检查是否为系统保留项
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public int SelYaxMachineEdit(string ID)
        {
            return dar.SelYaxMachineEdit(ID);
        }
    }
}
